#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Bootstrap UI Viewlet Module

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_45"
__date__    = "2014/05/04"
__license__ = 'LGPL'

# ------------------------------------------------------------
## Import Section
# ------------------------------------------------------------
# Python
import sys
import platform
import socket
import traceback
import calendar
from random import Random
from datetime import datetime
from collections import OrderedDict

# ESiPC
from esipc.calendar import get_holiday
from esipc import ESiPCProperty
from esipc.disk import stat_disks

# Mikan
from mikan.core import MIKAN
from mikan.viewlet.bootstrap import MIKAN_BOOTSTRAP, MikanBootstrapLinkItem, MikanBootstrapNavBarViewlet, \
	MikanBootstrapInlineViewlet, AbstractMikanBootstrapDivViewlet, MikanBootstrapIconViewlet, add_link_item2, MikanBootstrapPanelViewlet,\
	AbstractMikanBootstrapSpanViewlet, AbstractMikanBootstrapMenuItem, AbstractMikanBootstrapLinkItem,\
	AbstractMikanBootstrapDivViewlet
from mikan.viewlet import MikanDivViewlet, MikanUlViewlet, MikanAViewlet, MikanTableViewlet, MikanTrViewlet,\
	MikanThViewlet, MikanImgViewlet, MikanTdViewlet, MikanTheadViewlet, MikanTbodyViewlet, MikanTextViewlet, \
	MikanPViewlet, MikanLiViewlet, MikanSpanViewlet, MikanBrViewlet, MikanH1Viewlet, MikanH3Viewlet, MikanH5Viewlet, MikanButtonViewlet

# ------------------------------------------------------------
## Variable Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Mikan Contant
# ------------------------------------------------------------

# ------------------------------------------------------------
## Function Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Class Section
# ------------------------------------------------------------

class MikanBootstrapCalendarItem(MikanBootstrapLinkItem):

	def get_date(self):
		return self.FDate

	def set_date(self, value):
		self.FDate = value

	def __init__(self, title, date, href=None, active=False, disabled=False, icon=None):
		super(MikanBootstrapCalendarItem, self).__init__(title, href, active, disabled, icon)

		self.FDate = date

	# ----------------------------------------------------
	## Property
	date = ESiPCProperty(get_date, set_date, None)

class MikanMenusViewlet(MikanBootstrapNavBarViewlet):

	def set_menus(self, value):
		self.FMenus = value

	def get_menus(self):
		return self.FMenus

	def get_nav(self):
		return self.FNav

	def __init__(self, parent, menus):
		super(MikanMenusViewlet, self).__init__(parent, True)

		self.FMenus = menus

		div = MikanDivViewlet(self.container)
		div.clazz = "navbar-collapse collapse"

		self.FNav = MikanUlViewlet(div)
		self.nav.clazz = "nav navbar-nav"

	def build_menu(self, menu, parent):
		li = MikanLiViewlet(parent)
		a = MikanAViewlet(li,  href=menu.url, target=menu.target)
		if (menu.icon != None):
			img = MikanImgViewlet(a, alt=menu.title, src=menu.icon, clazz="icon")
		title = MikanTextViewlet(a, menu.title)

	def perform(self):
		for m in self.menus:
			li = MikanLiViewlet(self.nav)
			t = li
			if (len(m.menus) == 0):
				a = MikanAViewlet(li,  href=m.url, target=m.target)
				t = a
			else:
				li.clazz = "dropdown"
				a = MikanAViewlet(li,  href="#", target=m.target)
				a.clazz = "dropdown-toggle"
				a.set_attribute_value("data-toggle", "dropdown")
				t = a

				s = MikanSpanViewlet(a,  " ")
				s.clazz = "caret"
				s.sortkey = 10000
			if (m.icon != None):
				img = MikanImgViewlet(t, alt=m.title, src=m.icon, clazz="icon")
			title = MikanTextViewlet(t, m.title)
			if (len(m.menus) != 0):
				ul = MikanUlViewlet(li)
				ul.clazz = "dropdown-menu"
				for mc in m.menus:
					self.build_menu(mc, ul)

		return super(MikanMenusViewlet, self).perform()

	# ----------------------------------------------------
	## Property
	nav = ESiPCProperty(get_nav, None, None)
	menus = ESiPCProperty(get_menus, set_menus, None)

class MikanHeaderAreaViewlet(MikanMenusViewlet):

	def __init__(self, parent, menus):
		super(MikanHeaderAreaViewlet, self).__init__(parent, menus)
		self.clazz = "navbar"

		div = MikanDivViewlet(self.container)
		div.clazz = "navbar-header"
		div.sortkey = -10000
		a = MikanAViewlet(div,  href=self.request.wsgiurl + "/")
		a.clazz = "navbar-brand"
		img = MikanImgViewlet(a, alt=self.request.sitetitle, src=self.request.imageurl + self.request.sitelogo, clazz="logo")
		title = MikanTextViewlet(a, self.request.sitetitle)

		self.nav.clazz = "nav navbar-nav navbar-right"

	# ----------------------------------------------------
	## Property

class MikanFootAreaViewlet(AbstractMikanBootstrapDivViewlet):

	def get_auther(self):
		return self.FAuther

	def get_copyright(self):
		return self.FCopyright

	def get_confidential(self):
		return self.FConfidential

	def get_system(self):
		return self.FSystem

	def get_version(self):
		return self.FVersion

	def get_container(self):
		return self.FContainer

	def __init__(self, parent, container=True):
		super(MikanFootAreaViewlet, self).__init__(parent)
		self.clazz = "footer"

		jumbotron = MikanDivViewlet(self)
		jumbotron.clazz = "jumbotron"

		if container:
			self.FContainer = MikanDivViewlet(jumbotron)
			self.container.clazz = "container"
			self.role = "contentinfo"
		else:
			self.FContainer = jumbotron

		self.FVersion = MikanH5Viewlet(self.container, u"%s Version.%s" % (self.request.sitesystem, self.request.siteversion))
		self.version.clazz = "text-right"
		self.version.sortkey = 10000
		self.FSystem = MikanH5Viewlet(self.container, self.request.sitesystem)
		self.system.isvisible = False
		self.system.clazz = "text-right"
		self.system.sortkey = 10000
		self.FAuther = MikanH5Viewlet(self.container, self.request.siteauther)
		self.auther.clazz = "text-right"
		self.auther.sortkey = 10100
		self.FCopyright = MikanH5Viewlet(self.container, self.request.sitecopyright)
		self.copyright.clazz = "text-right"
		self.copyright.sortkey = 10200
		self.FConfidential = MikanH5Viewlet(self.container, self.request.siteconfidential)
		self.confidential.clazz = "text-right"
		self.confidential.sortkey = 10300

	# ----------------------------------------------------
	## Property
	container = ESiPCProperty(get_container, None, None)
	auther = ESiPCProperty(get_auther, None, None)
	copyright = ESiPCProperty(get_copyright, None, None)
	version = ESiPCProperty(get_version, None, None)
	system = ESiPCProperty(get_system, None, None)
	confidential = ESiPCProperty(get_confidential, None, None)

class AbstractMikanBootstrapInfoViewlet(MikanBootstrapPanelViewlet):

	def __init__(self, parent, title):
		super(AbstractMikanBootstrapInfoViewlet, self).__init__(parent, title, icon_name="tasks")

	def get_items(self):
		return []

	def perform(self):
		list = MikanBootstrapInlineViewlet(self)

		for title, value, progress in self.get_items():
			list.add_item(title, value)

		return super(AbstractMikanBootstrapInfoViewlet, self).perform()

class MikanBootstrapInfoViewlet(AbstractMikanBootstrapInfoViewlet):

	def get_items(self):
		return self.FItems

	def __init__(self, parent, title):
		super(MikanBootstrapInfoViewlet, self).__init__(parent, title)

		self.FItems = []

	def add_item(self, title, value, progress=None):
		self.items.append((title, value, progress))

	# ----------------------------------------------------
	## Property
	items = ESiPCProperty(get_items, None, None)

# ------------------------------------------------------------
## Mikan メニューバー ビューレットクラス
#
class MikanBootstrapSiteInfoViewlet(MikanBootstrapInfoViewlet):

	def __init__(self, parent):
		super(MikanBootstrapSiteInfoViewlet, self).__init__(parent, u"サイト情報")

	def perform(self):
		self.add_item(u"サイト名", "%s" % (self.request.sitetitle))
		self.add_item(u"サイト詳細", "%s" % (self.request.sitedescription))
		self.add_item(u"サイトバージョン", "%s" % (self.request.siteversion))
		self.add_item(u"サイト更新日", "%s" % (self.request.sitedate))
		self.add_item(u"サイトシステム名", "%s" % (self.request.sitesystem))
		self.add_item(u"サイト著作権", "%s" % (self.request.sitecopyright))
		self.add_item(u"サイトオーナ", "%s" % (self.request.siteauther))

		return super(MikanBootstrapSiteInfoViewlet, self).perform()

class MikanBootstrapPythonInfoViewlet(MikanBootstrapInfoViewlet):

	def __init__(self, parent):
		super(MikanBootstrapPythonInfoViewlet, self).__init__(parent, u"Python情報")

	def perform(self):
		self.add_item(u"Python", "%s %s" % (platform.python_implementation(), platform.python_version()))
		self.add_item(u"Pythont著作権", sys.copyright)
		self.add_item(u"Web Framework", "%s %s" % (MIKAN.MIKAN_NAME, MIKAN.MIKAN_VERSION))
		self.add_item(u"OS", "%s %s" % (platform.system(), platform.release()))
		self.add_item(u"マシンタイプ", platform.machine())
		self.add_item(u"ホスト名", platform.node())
		try:
			self.add_item(u"IPアドレス", socket.gethostbyname(socket.gethostname()))
		except:
			pass
		self.add_item(u"プロセッサタイプ", platform.processor())
		self.add_item(u"サーバー名", self.request.serversoftware)

		try:
			db = self.request.db

			try:
				self.add_item(u"データベース接続タイプ", "%s" % (db.name))
			except:
				pass

			try:
				self.add_item(u"データベースバージョン", "%s" % (db.version))
			except:
				pass
		except:
			pass

		return super(MikanBootstrapPythonInfoViewlet, self).perform()

class MikanBootstrapDiskInfoViewlet(MikanBootstrapInfoViewlet):

	def __init__(self, parent):
		super(MikanBootstrapDiskInfoViewlet, self).__init__(parent, u"ディスク情報")

	def perform(self):
		disks = stat_disks()
		try:
			for key, disk in disks.items():
				self.add_item(key, "%s/%s" % (disk["Used"], disk["Size"]), disk["Use%"])
		except:
			self.logger.debug(traceback.format_exc())

		return super(MikanBootstrapDiskInfoViewlet, self).perform()

class MikanBootstrapRequestInfoViewlet(MikanBootstrapInfoViewlet):

	def __init__(self, parent):
		super(MikanBootstrapRequestInfoViewlet, self).__init__(parent, u"リクエスト情報")

	def perform(self):
		self.add_item(u"サーバープロトコル", self.request.serverprotocol)
		self.add_item(u"CGI", self.request.gatewayinterface)
		self.add_item(u"ロケール", self.request.locale.__str__())
		self.add_item(u"タイムゾーン", self.request.timezone.__str__())

		for k in sorted(self.request.environ.keys()):
			self.add_item(k, self.request.environ[k])

		return super(MikanBootstrapRequestInfoViewlet, self).perform()

class MikanBootstrapCalendarViewlet(AbstractMikanBootstrapDivViewlet):

	CALENDER_TYPE_MONTH = 0
	CALENDER_TYPE_WEEK = 1

	def set_target_date(self, value):
		self.FTargetDate = value

	def get_target_date(self):
		return self.FTargetDate

	def set_type(self, value):
		self.FType = value

	def get_type(self):
		return self.FType

	def set_title(self, value):
		self.FTitle = value

	def get_title(self):
		return self.FTitle

	def get_items(self):
		return self.FItems

	def set_show_item(self, value):
		self.FShowItem = value

	def is_show_item(self):
		return self.FShowItem

	def set_week_text(self, value):
		self.FWeekText = value

	def get_week_text(self):
		return self.FWeekText

	def __init__(self, parent, type=CALENDER_TYPE_MONTH, title=None, items=None):
		super(MikanBootstrapCalendarViewlet, self).__init__(parent)
		self.clazz = "panel panel-default"

		self.FType = type
		self.FTitle = title
		self.FTargetDate = datetime.today()
		if items != None:
			self.FItems = items
		else:
			self.FItems = []
		self.FShowItem = True

	def get_item(self, year, month, day):
		result = []

		for item in self.items:
			if item.date != None:
				if item.date.year == year:
					if item.date.month == month:
						if item.date.day == day:
							result.append(item)
		return result

	def perform_month(self):
		table = self._perform_title()

		tbody = MikanTbodyViewlet(table)

		c = calendar.Calendar(calendar.SUNDAY)

		dw = c.monthdays2calendar(self.targetdate.year, self.targetdate.month)
		for weeks in dw:
			self._perform_week_day(tbody, weeks)

	def _is_found(self, weeks, day):
		for d, w in weeks:
			if d == day:
				return True
		return False

	def _perform_title(self):

		t = ""
		if self.title != None:
			t = self.title
		t = u"%s %d年 %d月" % (t, self.targetdate.year, self.targetdate.month)
		title = MikanDivViewlet(self, t)
		title.clazz = "panel-heading"
		MikanBootstrapIconViewlet(title, "calendar")

		table = MikanTableViewlet(self)
		table.clazz = "table table-bordered fixed"

		thead = MikanTheadViewlet(table)
		tr = MikanTrViewlet(thead)
		self.weektext = u"曜日"
		MikanThViewlet(tr, nodevalue=u"日" + self.weektext).clazz = "text-center danger"
		MikanThViewlet(tr, nodevalue=u"月" + self.weektext).clazz = "text-center"
		MikanThViewlet(tr, nodevalue=u"火" + self.weektext).clazz = "text-center"
		MikanThViewlet(tr, nodevalue=u"水" + self.weektext).clazz = "text-center"
		MikanThViewlet(tr, nodevalue=u"木" + self.weektext).clazz = "text-center"
		MikanThViewlet(tr, nodevalue=u"金" + self.weektext).clazz = "text-center"
		MikanThViewlet(tr, nodevalue=u"土" + self.weektext).clazz = "text-center info"

		return table

	def _perform_week_day(self, tbody, weeks):
		now = datetime.now()

		tr = MikanTrViewlet(tbody)
		for d,w in weeks:
			td = MikanTdViewlet(tr)
			holiday = None
			if d != 0:
				holiday = get_holiday(self.targetdate.year, self.targetdate.month, d)
				if holiday != None:
					t = u"%d %s" % (d, holiday)
				else:
					t = str(d)
				MikanH5Viewlet(td, t)
			else:
				td.clazz = "gray-light"

			if (now.year == self.targetdate.year) and (now.month == self.targetdate.month) and (now.day == d):
				td.clazz = "warning"
			elif holiday != None:
				td.clazz = "danger"
			elif w == 6:
				td.clazz = "danger"
			elif w == 5:
				td.clazz = "info"

			items = []
			if self.isshowitem:
				items = self.get_item(self.targetdate.year, self.targetdate.month, d)
			if len(items) > 0:
				for i in items:
					add_link_item2(MikanPViewlet, td, i)
			else:
				MikanBrViewlet(MikanPViewlet(td))

	def perform_week(self):
		table = self._perform_title()

		tbody = MikanTbodyViewlet(table)

		c = calendar.Calendar(calendar.SUNDAY)

		dw = c.monthdays2calendar(self.targetdate.year, self.targetdate.month)
		for weeks in dw:
			if self._is_found(weeks, self.targetdate.day):
				self._perform_week_day(tbody, weeks)

	def perform(self):
		if self.type == MikanBootstrapCalendarViewlet.CALENDER_TYPE_WEEK:
			self.perform_week()
		else:
			self.perform_month()
		return super(MikanBootstrapCalendarViewlet, self).perform()

	# ----------------------------------------------------
	## Property
	weektext = ESiPCProperty(get_week_text, set_week_text, None)
	isshowitem = ESiPCProperty(is_show_item, set_show_item, None)
	items = ESiPCProperty(get_items, None, None)
	type = ESiPCProperty(get_type, set_type, None)
	title = ESiPCProperty(get_title, set_title, None)
	targetdate = ESiPCProperty(get_target_date, set_target_date, None)

class MikanBootstrapIconBarViewlet(AbstractMikanBootstrapSpanViewlet):

	def __init__(self, parent):
		super(MikanBootstrapIconBarViewlet, self).__init__(parent)
		self.clazz = "icon-bar"

class MikanBootstrapToggleNavigationViewlet(MikanButtonViewlet, MIKAN_BOOTSTRAP):

	def __init__(self, parent):
		super(MikanBootstrapToggleNavigationViewlet, self).__init__(parent)
		self.type = "button"
		self.clazz = "navbar-toggle"
		self.set_attribute_value("data-toggle", "collapse")
		self.set_attribute_value("data-target", ".navbar-collapse")

		s = MikanSpanViewlet(self)
		s.clazz = "sr-only"
		s.nodevalue = "Toggle navigation"

		MikanBootstrapIconBarViewlet(self)
		MikanBootstrapIconBarViewlet(self)
		MikanBootstrapIconBarViewlet(self)

class MikanBootstrapSideBarMenuItem(AbstractMikanBootstrapMenuItem):

	def __init__(self, title,name=None,hint=None,href="#",target=None,icon_name=None,icon_clazz="glyphicon",active=False,disabled=False,click=None,clazz=None):
		super(MikanBootstrapSideBarMenuItem, self).__init__(title=title,hint=hint,href=href,target=target,icon_name=icon_name,icon_clazz=icon_clazz,active=active,disabled=disabled,click=click,clazz=clazz)

		self.FName = name
		self.FMenus = []

		self.FSubItem = MikanBootstrapSideBarMenuSubItem()

	def get_name(self):
		return self.FName

	def set_name(self, value):
		self.FName = value

	def perform(self, ul, level=None):
		li = MikanLiViewlet(ul)
		a = MikanAViewlet(li)
		if self.iconname:
			icon = MikanBootstrapIconViewlet(a, self.iconname, self.iconclazz)
			icon.sortkey = -1
		if self.active:
			a.clazz = "active"
		if self.target:
			a.target = self.target
		if self.href:
			a.href = self.href
		MikanTextViewlet(a, u" %s " % self.title)

		if len(self.menus) > 0:
			icon = MikanBootstrapIconViewlet(a, "caret-down", "fa")
		self.subitem.perform(a)

		if len(self.menus) > 0:
			le = level
			u = MikanUlViewlet(li)
			if not  le:
				u.clazz = "nav nav-second-level"
			else:
				u.clazz = "nav nav-third-level"

			for menu in self.menus:
				menu.perform(u, True)

	def get_menus(self):
		return self.FMenus

	def get_sub_item(self):
		return self.FSubItem

	# ----------------------------------------------------
	## Property
	menus = ESiPCProperty(get_menus, None, None)
	subitem = ESiPCProperty(get_sub_item, None, None)
	name = ESiPCProperty(get_name, set_name, None)

class MikanBootstrapSideBarMenuSubItem(AbstractMikanBootstrapLinkItem):

	def __init__(self, title=None,name=None,icon_name=None,icon_clazz="glyphicon",clazz=None):
		super(MikanBootstrapSideBarMenuSubItem, self).__init__(title=title,icon_name=icon_name,icon_clazz=icon_clazz,clazz=clazz)

	def perform(self, a):
		if self.iconname:
			icon = MikanBootstrapIconViewlet(a, self.iconname, self.iconclazz)
		else:
			if self.title:
				span = MikanSpanViewlet(a)
				span.clazz = self.clazz
				span.nodevalue = self.title

class MikanSideBarViewlet(AbstractMikanBootstrapDivViewlet):

	def __init__(self, parent):
		super(MikanSideBarViewlet, self).__init__(parent)
		self.clazz = "navbar-default sidebar"
		self.role = "navigation"
		self.sortkey = -999

		self.FMenus = []

		self.s = MikanDivViewlet(self)
		self.s.clazz = "sidebar-nav navbar-collapse"

	def get_menus(self):
		return self.FMenus

	def perform(self):
		n = MikanUlViewlet(self.s)
		n.clazz = "nav"
		n.id = "side-menu"

		for menu in self.menus:
			menu.perform(n)

		return super(MikanSideBarViewlet, self).perform()

	# ----------------------------------------------------
	## Property
	menus = ESiPCProperty(get_menus, None, None)

class MikanTilePanelViewlet(AbstractMikanBootstrapDivViewlet):
	def __init__(self, parent, title=None, subtitle=None, hint=None, href=None, target=None, click=None, panel_type="green",icon_name=None,icon_clazz="glyphicon"):
		super(MikanTilePanelViewlet, self).__init__(parent)

		self.FIconName = icon_name
		self.FIconClazz = icon_clazz
		self.FTitle = title
		self.FSubTitle = subtitle
		self.FPanelType = panel_type
		self.FClick = click
		self.FHint = hint
		self.FHref = href
		self.FTarget = target

	def get_icon_name(self):
		return self.FIconName
	def get_icon_clazz(self):
		return self.FIconClazz
	def get_title(self):
		return self.FTitle
	def get_subtitle(self):
		return self.FSubTitle
	def get_panel_type(self):
		return self.FPanelType
	def get_click(self):
		return self.FClick
	def get_hint(self):
		return self.FHint
	def get_href(self):
		return self.FHref
	def get_target(self):
		return self.FTarget

	def set_icon_name(self, value):
		self.FIconName = value
	def set_icon_clazz(self, value):
		self.FIconClazz = value
	def set_title(self, value):
		self.FTitle = value
	def set_subtitle(self, value):
		self.FSubTitle = value
	def set_panel_type(self, value):
		self.FPanelType = value
	def set_click(self, value):
		self.FClick = value
	def set_hint(self, value):
		self.FHint = value
	def set_href(self, value):
		self.FHref = value
	def set_target(self, value):
		self.FTarget = value

	def perform(self):
		self.clazz = "panel panel-%s" % self.paneltype
		header = MikanDivViewlet(self)
		header.clazz = "panel-heading"

		row = MikanDivViewlet(header)
		row.clazz = "row"
		div = MikanDivViewlet(row)
		div.clazz = "col-xs-2"
		h1 = MikanH1Viewlet(div)
		icon = MikanBootstrapIconViewlet(h1, self.iconname, self.iconclazz)

		div = MikanDivViewlet(row)
		div.clazz = "col-xs-10 text-center"
		if self.title:
			MikanDivViewlet(div, nodevalue=self.title, clazz="huge")
		if self.subtitle:
			MikanDivViewlet(div, nodevalue=self.subtitle)

		a = MikanAViewlet(self)
		if self.href:
			a.href = self.href
		a.target = self.target
		a.click = self.click
		footer = MikanDivViewlet(a)
		footer.clazz = "panel-footer"
		if self.hint:
			footer.nodevalue = self.hint

		return super(MikanTilePanelViewlet, self).perform()

	# ----------------------------------------------------
	## Property
	iconname = ESiPCProperty(get_icon_name, set_icon_name, None)
	iconclazz = ESiPCProperty(get_icon_clazz, set_icon_clazz, None)
	title = ESiPCProperty(get_title, set_title, None)
	subtitle = ESiPCProperty(get_subtitle, set_subtitle, None)
	paneltype = ESiPCProperty(get_panel_type, set_panel_type, None)
	click = ESiPCProperty(get_click, set_click, None)
	hint = ESiPCProperty(get_hint, set_hint, None)
	href = ESiPCProperty(get_href, set_href, None)
	target = ESiPCProperty(get_target, set_target, None)

class MikanBootstrapMenuBarMenuItem(AbstractMikanBootstrapMenuItem):

	def __init__(self, title,hint=None,href="#",target=None,icon_name=None,icon_clazz="glyphicon",active=False,disabled=False,click=None,clazz=None):
		super(MikanBootstrapMenuBarMenuItem, self).__init__(title=title,hint=hint,href=href,target=target,icon_name=icon_name,icon_clazz=icon_clazz,active=active,disabled=disabled,click=click,clazz=clazz)

		self.FMenus = []
		self.FSubItem = MikanBootstrapSideBarMenuSubItem()

	def perform(self, ul):
		li = MikanLiViewlet(ul)
		if self.title:
			a = MikanAViewlet(li)
			if self.iconname:
				icon = MikanBootstrapIconViewlet(a, self.iconname, self.iconclazz)
				icon.sortkey = -1
			if self.active:
				a.clazz = "active"
			if self.target:
				a.target = self.target
			if self.href:
				a.href = self.href
			if self.click:
				a.onclick = self.click
			if self.clazz:
				li.clazz = self.clazz
				a.clazz = self.clazz
			MikanTextViewlet(a, u" %s " % self.title)

			self.subitem.perform(a)
		else:
			li.clazz = "divider"

	def get_menus(self):
		return self.FMenus

	def get_sub_item(self):
		return self.FSubItem

	# ----------------------------------------------------
	## Property
	subitem = ESiPCProperty(get_sub_item, None, None)
	menus = ESiPCProperty(get_menus, None, None)

class MikanMenuBarViewlet(AbstractMikanBootstrapDivViewlet):

	def __init__(self, parent):
		super(MikanMenuBarViewlet, self).__init__(parent)
		self.clazz = "btn-group"

		self.FMenus = []

	def get_menus(self):
		return self.FMenus

	def perform(self):
		for menu in self.menus:
			d = MikanDivViewlet(self)
			d.clazz = "btn-group"
			b = MikanButtonViewlet(d)
			b.clazz = "btn btn-default dropdown-toggle"
			b.set_attribute_value("data-toggle", "dropdown")
			if menu.iconname:
				icon = MikanBootstrapIconViewlet(b, menu.iconname, menu.iconclazz)
				icon.sortkey = -1
			MikanTextViewlet(b, u" %s " % menu.title)
			MikanSpanViewlet(b, clazz="caret")

			ul = MikanUlViewlet(d)
			ul.clazz = "dropdown-menu"
			ul.role = "menu"
			for m in menu.menus:
				m.perform(ul)

		return super(MikanMenuBarViewlet, self).perform()

	# ----------------------------------------------------
	## Property
	menus = ESiPCProperty(get_menus, None, None)

# ------------------------------------------------------------
## Main Section
# ------------------------------------------------------------

# ------------------------------------------------------------
